package com.example.JcGQS.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.JcGQS.mapper.CategoryMapper;
import com.example.JcGQS.pojo.dto.DeleteCategoryDTO;
import com.example.JcGQS.pojo.dto.InsertCategoryDTO;
import com.example.JcGQS.pojo.dto.UpdateCategoryDTO;
import com.example.JcGQS.pojo.entity.GoodsCategory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 物品类型Dao
 * @author 末日
 * @date 2023/11/21
 */
@Component
public class CategoryDao {
    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 新增物品类型
     */
    public void insertCategory(GoodsCategory category) {
        categoryMapper.insert(category);
    }

    /**
     * 删除物品类型
     */
    public void deleteCategory(DeleteCategoryDTO deleteCategoryDTO) {
        categoryMapper.deleteById(deleteCategoryDTO.getCategoryId());
    }

    /**
     * 修改物品类型
     */
    public void updateCategory(GoodsCategory category) {
        UpdateWrapper<GoodsCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("category_id", category.getCategoryId());
        categoryMapper.update(category, updateWrapper);
    }

    /**
     * 查询物品类型列表
     */

    public List<GoodsCategory> categoryList() {
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("category_id");
        return categoryMapper.selectList(queryWrapper);
    }

    /**
     * 根据id查询物品类型数据
     */
    public Integer categorySelect(Integer id) {
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", id);
        return categoryMapper.selectCount(queryWrapper);
    }

    /**
     * 根据id查询物品类型数据
     */
    public GoodsCategory categorySelectName(String name) {
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name", name);
        return categoryMapper.selectOne(queryWrapper);
    }
}
